﻿using System;
using System.Collections.Generic;


namespace ProductInfo.Core.Service
{
	public class ReleaseNoteEntryService : ProductInfo.Core.Service.IReleaseNoteEntryService
	{
		private readonly ProductInfo.Core.Data.Repository.IReleaseNoteEntry c_releaseNoteEntryRepository;
		private readonly ProductInfo.Core.Service.IValidationErrorCollection c_validationErrorCollection;


		public IDictionary<string, string> ValidationErrors { get { return this.c_validationErrorCollection.Errors; } }


		public ReleaseNoteEntryService(
			ProductInfo.Core.Data.Repository.IReleaseNoteEntry releaseNoteEntryRepository,
			ProductInfo.Core.Service.IValidationErrorCollection validationErrorCollection)
		{
			this.c_releaseNoteEntryRepository = releaseNoteEntryRepository;
			this.c_validationErrorCollection = validationErrorCollection;
		}


		public IEnumerable<ProductInfo.Core.Models.ReleaseNoteEntry> List()
		{
			return this.c_releaseNoteEntryRepository.FetchAll();
		}


		public ProductInfo.Core.Models.ReleaseNoteEntry Edit(
			int key)
		{
			return this.c_releaseNoteEntryRepository.FetchByKey(key);
		}


		public bool Update(
			ProductInfo.Core.Models.ReleaseNoteEntry releaseNoteEntry)
		{
			if (!this.ValidateReleaseNoteEntry(releaseNoteEntry)) { return false; }

			this.c_releaseNoteEntryRepository.Update(releaseNoteEntry);

			return true;
		}


		public bool Create(
			ProductInfo.Core.Models.ReleaseNoteEntry releaseNoteEntry)
		{
			if (!this.ValidateReleaseNoteEntry(releaseNoteEntry)) { return false; } 

			this.c_releaseNoteEntryRepository.Create(releaseNoteEntry);

			return true;
		}


		public void UpdateVersion(
			string originalValue, 
			string updateValue)
		{
			this.c_releaseNoteEntryRepository.Update(originalValue, updateValue);
		}


		public List<string> FetchDistinctVersions()
		{
			var _result = new List<string>();
			var _releaseNoteEntries = this.c_releaseNoteEntryRepository.FetchAll();

			foreach (var _releaseNoteEntry in _releaseNoteEntries)
			{
				if (!_result.Contains(_releaseNoteEntry.Version)) { _result.Add(_releaseNoteEntry.Version); }
			}

			return _result;
		}


		private bool ValidateReleaseNoteEntry(
			ProductInfo.Core.Models.ReleaseNoteEntry releaseNoteEntry)
		{
			//TODO Need to do proper validation here
			if (releaseNoteEntry.ProductId <= 0)
			{
				this.c_validationErrorCollection.AddError("ProductId", "* Cannot be less than zero");
			}
			if (string.IsNullOrEmpty(releaseNoteEntry.Version) || releaseNoteEntry.Version.Trim().Length == 0)
			{
				this.c_validationErrorCollection.AddError("Version", "* Required");
			}
			if (string.IsNullOrEmpty(releaseNoteEntry.Detail) || releaseNoteEntry.Detail.Trim().Length == 0)
			{
				this.c_validationErrorCollection.AddError("Detail", "* Required");
			}
			if (string.IsNullOrEmpty(releaseNoteEntry.Author) || releaseNoteEntry.Author.Trim().Length == 0)
			{
				this.c_validationErrorCollection.AddError("Author", "* Required");
			}

			return this.c_validationErrorCollection.IsValid;
		}
	}
}